package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsBrand;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author SiXiaoLong
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    PmsBrandService pmsBrandService;

    /**
     * 获取分页列表
     */

    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProductCategory>> list(
                                                             @RequestParam(value = "pageNum") Integer pageNum,
                                                             @RequestParam(value = "pageSize") Integer pageSize,
                                                             @RequestParam(value = "keyword", required = false) String keyword){

        Page page = pmsBrandService.getList(pageNum, pageSize, keyword);
        return CommonResult.success(CommonPage.restPage(page));
//        return CommonResult<CommonPage<PmsProductCategory>>.success(restPage);
    }

    /**
     * 是否显示
     */
    @PostMapping("/update/showStatus")
    public CommonResult showStatus(@RequestBody Map map){
        System.out.println(map.get("ids").toString());
        System.out.println(map.get("showStatus").toString());
        System.out.println(map);
        List<Long> ids = (List<Long>) map.get("ids");
        LambdaUpdateWrapper<PmsBrand> luw = new LambdaUpdateWrapper<>();
        luw.set(PmsBrand::getShowStatus,map.get("showStatus").toString());
        luw.in(PmsBrand::getId, ids);
        boolean update = pmsBrandService.update(luw);
        if (update){
            return CommonResult.success(update);
        }else {
            return CommonResult.failed("修改失败");
        }
    }
    /**
     * 删除品牌
     */
    @GetMapping("/delete/{id}")
    public CommonResult showStatus(@PathVariable Long id){
        boolean b = pmsBrandService.removeById(id);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("删除失败");
        }
    }
}

